package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.CustomType;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStoreCommodityType;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.service.ICustomTypeService;
import com.cz.zslu.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 自定义类型 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-12-05
 */
@RestController
@RequestMapping("/sys/customType")
@Api(description = "商家自定义类型")
public class CustomTypeController {
    @Autowired
    private ICustomTypeService iCustomTypeService;
    @Autowired
    private ISysUserService iSysUserService;


    @ApiOperation("获取商家分类（树）")
    @IgnoreAuth
    @RequestMapping(value = "/getAllList", method = RequestMethod.POST)
    public ReturnObject<List<CustomType>> getAllList(@ApiParam("商户id") @RequestParam Long storeId) {

        EntityWrapper<CustomType> ew = new EntityWrapper<>();
        ew.orderBy("sort", true);
        ew.isNull("p_id");
        ew.eq("store_id", storeId);
        List<CustomType> obj = iCustomTypeService.selectList(ew);
        for (int i = 0; i < obj.size(); i++) {
            obj.get(i).setSubordinate(iCustomTypeService.selectList(new EntityWrapper<CustomType>().eq("store_id",
                    storeId).eq("p_id", obj.get(i).getId()).orderBy("sort",true)));
        }
        return new ReturnObject<>(0, "获取成功", obj);
    }

    @IgnoreAuth
    @ApiOperation("获取商家自定义分类")
    @RequestMapping(value = "/getCustomType", method = RequestMethod.POST)
    public ReturnObject<List<CustomType>> getCustomType(@ApiParam("商户id") @RequestParam Long storeId) {
        return new ReturnObject<>(0, "获取成功", iCustomTypeService.selectList(new EntityWrapper<CustomType>().eq(
                "store_id", storeId)));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/addCustomType", method = RequestMethod.POST)
    public ReturnObject addCustomType(@ApiParam("名称") @RequestParam String name,
                                      @ApiParam("上级id") Long pId) {

        int newSort = 0;
        List<CustomType> list = null;
        if (pId != null) {
            list = iCustomTypeService.selectList(new EntityWrapper<CustomType>().eq("user_id", BaseContextHandler.getUserID()).eq("p_id", pId).orderBy("sort", false));
        } else {
            list = iCustomTypeService.selectList(new EntityWrapper<CustomType>().eq("user_id", BaseContextHandler.getUserID()).isNull("p_id").orderBy("sort", false));
        }
        if (list != null && list.size() > 0) {
            newSort = list.get(0).getSort() + 1;
        }
        CustomType info = new CustomType();
        info.setName(name);
        info.setPId(pId);
        info.setUserId(BaseContextHandler.getUserID());
        info.setSort(newSort);
        info.setStoreId(iSysUserService.selectById(BaseContextHandler.getUserID()).getStoreId());
        iCustomTypeService.insert(info);
        return new ReturnObject<>(0, "新增成功");
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/editCustomType", method = RequestMethod.POST)
    public ReturnObject editCustomType(@ApiParam("名称") @RequestParam String name,
                                       @ApiParam("id") @RequestParam Long id) {

        CustomType info = iCustomTypeService.selectById(id);
        info.setName(name);
        iCustomTypeService.updateById(info);
        return new ReturnObject<>(0, "编辑成功");
    }

    @ApiOperation("修改排序")
    @RequestMapping(value = "/editSort", method = RequestMethod.POST)
    public ReturnObject editSort(
            @ApiParam("id") @RequestParam Long id,
            @ApiParam("0：向下移 1：向上移") Integer move) {
        CustomType info = iCustomTypeService.selectById(id);
        int currentIndex = 0;
        int nextSort = 0;
        if (info.getPId() != null) {
            List<CustomType> pidCustomType = iCustomTypeService.selectList(new EntityWrapper<CustomType>().eq("p_id", info.getPId())
                    .eq("user_id", BaseContextHandler.getUserID()).orderBy("sort", true));
            for (int i = 0; i < pidCustomType.size(); i++) {
                if (info.getId().longValue()== pidCustomType.get(i).getId().longValue()) {
                    currentIndex = i;
                    CustomType nextCustomType = null;
                    if (move == 1) {
                        nextCustomType = pidCustomType.get(currentIndex - 1);
                        nextSort = nextCustomType.getSort();
                        nextCustomType.setSort(info.getSort());

                    } else {
                        nextCustomType = pidCustomType.get(currentIndex +1);
                        nextSort = nextCustomType.getSort();
                        nextCustomType.setSort(info.getSort());

                    }
                    iCustomTypeService.updateById(nextCustomType);
                    info.setSort(nextSort);
                    iCustomTypeService.updateById(info);
                }
            }

        } else {
            List<CustomType> noPidCustomType = iCustomTypeService.selectList(new EntityWrapper<CustomType>().isNull("p_id")
                    .eq("user_id", BaseContextHandler.getUserID()).orderBy("sort", true));

            for (int i = 0; i < noPidCustomType.size(); i++) {
                if (info.getId().longValue() == noPidCustomType.get(i).getId().longValue()) {
                    currentIndex = i;

                    CustomType nextCustomType = null;
                    if (move == 1) {

                        nextCustomType = noPidCustomType.get(currentIndex - 1);
                        nextSort = nextCustomType.getSort();
                        nextCustomType.setSort(info.getSort());

                    } else {

                        nextCustomType = noPidCustomType.get(currentIndex + 1);
                        nextSort = nextCustomType.getSort();
                        nextCustomType.setSort(info.getSort());

                    }
                    iCustomTypeService.updateById(nextCustomType);
                    info.setSort(nextSort);
                    iCustomTypeService.updateById(info);
                }
            }
        }


        return new ReturnObject<>(0, "编辑成功");
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public ReturnObject<ZslStoreCommodityType> delById(@ApiParam("id") @RequestParam Long id) {
        iCustomTypeService.deleteById(id);
        return new ReturnObject<>(0, "删除成功", null);
    }


}

